﻿namespace Blaze.ConfigExportTool.Serialization
{
    using System;

    /// <summary>
    /// 提供将对象序列化到流的功能。
    /// </summary>
    public class LuaSerializer : SerializerBase
    {
        public LuaSerializer(Type targetType)
            : base(targetType)
        {
        }

        /// <summary>
        /// 获取数组数据块。
        /// </summary>
        protected override Block GetCollectionBlock(bool newLine)
        {
            return new Block(this, "{", "}", newLine);
        }

        /// <summary>
        /// 获取字典数据块。
        /// </summary>
        protected override Block GetMapBlock(bool newLine)
        {
            return new Block(this, "{", "}", newLine);
        }

        /// <summary>
        /// 当需要序列化时调用此方法。
        /// </summary>
        /// <param name="target">需要序列化的对象</param>
        protected override void OnSerialize(object target)
        {
            WriteLine("local M = ");
            Write(TargetType, target);
            WriteLine();
            WriteLine("return M");
        }

        /// <summary>
        /// 写入赋值符号。 如：在lua中为"="，在python中为":"。
        /// </summary>
        protected override void WriteAssignmentSymbol()
        {
            Writer.Write(" = ");
        }
    }
}